package net.droiding.service;

import java.util.Timer;
import java.util.TimerTask;

import net.droiding.R;
import net.droiding.activity.DroidingActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class DroidingService extends Service {

	private Timer timer = new Timer();
	private static final int NOTIFICATION_ID = 1000;

    private NotificationManager notificationManager = null;
    private final IBinder binder = new DroidingBinder();
    

    public class DroidingBinder extends Binder {
    	public DroidingService getService() {
            return DroidingService.this;
        }
    }
	
	@Override
	public void onCreate() {
        super.onCreate();
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		startService();
	}
	@Override
	public void onDestroy() {
		super.onDestroy();
		stopService();
	}
	
	private void startService() {
		timer.scheduleAtFixedRate(
				new TimerTask() {
					public void run() {
						showNotification("downloading....", "downloading new bicing infos !");
					}
				}, 0, 3000);
		
	}
	private void stopService() {
		if (timer != null)
			timer.cancel();
	}
    
	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}
    private void showNotification(String title, String description) {

        // Set the icon, scrolling text and timestamp
        Notification notification = new Notification(R.drawable.bicing, title,
                System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, DroidingActivity.class), 0);

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, title, description, contentIntent);

        // Send the notification.
        // We use a layout id because it is a unique number.  We use it later to cancel.
        notificationManager.notify(NOTIFICATION_ID, notification);
    }	
}
